/*
 * author：yuanfeng
 * mail: yuanfeng1897@qq.com / yuanfeng1897@gmail.com
 *
 * 注：支持C++11及以上版本
*/

#ifndef SINGLETON_H
#define SINGLETON_H

#include <memory>
#include <mutex>

/*
 * @brief 使用宏来创建单例
 * @note  此处声明了构造函数为默认无参形式
 * @note  此处禁用了拷贝构造函数和赋值函数
 * @note  此处禁用了转移构造函数和转移赋值函数
*/
#define SINGLETON(Class)                                \
public:                                                 \
static Class *Instance() {                              \
    static std::shared_ptr<Class> instance{nullptr};    \
    static std::once_flag s_flag;                       \
    std::call_once(s_flag, [&]()                        \
    {                                                   \
    instance.reset(new Class, [](Class *p){delete p;}); \
    });                                                 \
    return instance.get();                              \
}                                                       \
private:                                                \
friend std::shared_ptr<Class>;                          \
Class() = default;                                      \
~Class() = default;                                     \
Class(const Class &other) = delete;                     \
Class& operator=(const Class &other) = delete;          \
Class(Class &&other) = delete;                          \
Class& operator=(Class &&other) = delete;               \

#endif // SINGLETON_H



